using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class FactorCustomersTodayDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public FactorCustomersTodayDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the FactorCustomersToday table.
		/// </summary>
		public virtual void Insert(FactorCustomersTodayInfo factorCustomersTodayInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdFactorCustomer", factorCustomersTodayInfo.IdFactorCustomer),
				new SqlParameter("@PostDate", factorCustomersTodayInfo.PostDate),
				new SqlParameter("@IdCommon", factorCustomersTodayInfo.IdCommon),
				new SqlParameter("@AccountNo", factorCustomersTodayInfo.AccountNo),
				new SqlParameter("@IMCustomer", factorCustomersTodayInfo.IMCustomer),
				new SqlParameter("@AdjustRate", factorCustomersTodayInfo.AdjustRate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "FactorCustomersToday_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the FactorCustomersToday table.
		/// </summary>
		public virtual void Update(FactorCustomersTodayInfo factorCustomersTodayInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdFactorCustomer", factorCustomersTodayInfo.IdFactorCustomer),
				new SqlParameter("@PostDate", factorCustomersTodayInfo.PostDate),
				new SqlParameter("@IdCommon", factorCustomersTodayInfo.IdCommon),
				new SqlParameter("@AccountNo", factorCustomersTodayInfo.AccountNo),
				new SqlParameter("@IMCustomer", factorCustomersTodayInfo.IMCustomer),
				new SqlParameter("@AdjustRate", factorCustomersTodayInfo.AdjustRate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "FactorCustomersToday_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the FactorCustomersToday table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idFactorCustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdFactorCustomer", idFactorCustomer)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "FactorCustomersToday_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the FactorCustomersToday table.
		/// </summary>
		public virtual FactorCustomersTodayInfo Select(Guid? idFactorCustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdFactorCustomer", idFactorCustomer)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "FactorCustomersToday_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeFactorCustomersTodayInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the FactorCustomersToday table.
		/// </summary>
		public List<FactorCustomersTodayInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "FactorCustomersToday_SelectAll"))
			{
				List<FactorCustomersTodayInfo> factorCustomersTodayInfoList = new List<FactorCustomersTodayInfo>();
				while (dataReader.Read())
				{
					factorCustomersTodayInfoList.Add(MakeFactorCustomersTodayInfo(dataReader));
				}

				return factorCustomersTodayInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the FactorCustomersToday class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual FactorCustomersTodayInfo MakeFactorCustomersTodayInfo(SqlDataReader dataReader)
		{
			FactorCustomersTodayInfo factorCustomersTodayInfo = new FactorCustomersTodayInfo();
			factorCustomersTodayInfo.IdFactorCustomer = SqlClientUtility.GetGuid(dataReader, DbConstants.FACTORCUSTOMERSTODAY.IDFACTORCUSTOMER);
			factorCustomersTodayInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.FACTORCUSTOMERSTODAY.POSTDATE);
			factorCustomersTodayInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.FACTORCUSTOMERSTODAY.IDCOMMON);
			factorCustomersTodayInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.FACTORCUSTOMERSTODAY.ACCOUNTNO);
			factorCustomersTodayInfo.IMCustomer = SqlClientUtility.GetDecimal(dataReader, DbConstants.FACTORCUSTOMERSTODAY.IMCUSTOMER);
			factorCustomersTodayInfo.AdjustRate = SqlClientUtility.GetDecimal(dataReader, DbConstants.FACTORCUSTOMERSTODAY.ADJUSTRATE);

			return factorCustomersTodayInfo;
		}

		#endregion
	}
}
